/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.StockDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.model.Stock;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class StockDaoImpl extends BaseDaoImpl implements StockDao {
	public Stock get(long productId, long storehouseId) {
		String sql = "from com.minierp.model.Stock as obj where obj.productId = "
				+ productId
				+ " and obj.storehouseId="
				+ storehouseId
				+ " order by obj.id";
		List list = this.getHibernateTemplate().find(sql);
		return list.isEmpty() ? null : (Stock) list.get(0);
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.Stock where id=:id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public List<?> getInOutList(long storehouseId, Date startDate,
			Date endDate, long proTypeId, long proId, String proCode,
			String proName, String bbCode, String type, long inTypeId,
			long outTypeId, long wcId, String listType, String orderCode) {
		if (inTypeId != 0L && outTypeId != 0L) {
			return new ArrayList();
		} else {
			if (type != null && type.equals("IN")) {
				if (outTypeId != 0L) {
					return new ArrayList();
				}
			} else if (type != null && type.equals("OUT") && inTypeId != 0L) {
				return new ArrayList();
			}

			String selectStr = "select io,p.productCode,p.productName,p.unit,p.productTypeId";
			String fromStr = "from com.minierp.model.InOutHistory io,com.minierp.model.Product p";
			String whereStr = "where io.proId=p.id";
			if (proTypeId != 0L) {
				fromStr = fromStr + ",com.minierp.model.ProductType t";
				whereStr = whereStr + " and p.productTypeId=t.id and (t.id="
						+ proTypeId + " or (t.parentIdStr like \'%;"
						+ proTypeId + ";%\'))";
			}

			if (storehouseId != 0L) {
				whereStr = whereStr + " and io.stoId=" + storehouseId;
			}

			if (listType.equals("LOG")) {
				if (startDate != null) {
					whereStr = whereStr + " and io.ioTime>=:startDate";
				}

				if (endDate != null) {
					whereStr = whereStr + " and io.ioTime<:endDate";
				}
			} else {
				whereStr = whereStr + " and io.logData=\'N\'";
				if (startDate != null) {
					whereStr = whereStr + " and io.billDate>=:startDate";
				}

				if (endDate != null) {
					whereStr = whereStr + " and io.billDate<:endDate";
				}
			}

			if (proId != 0L) {
				whereStr = whereStr + " and p.id=" + proId;
			}

			if (proCode != null) {
				proCode = proCode.trim();
			}

			if (proCode != null && !proCode.equals("")) {
				proCode = proCode.replace("\'", "\'\'");
				whereStr = whereStr + " and p.productCode like \'%" + proCode
						+ "%\'";
			}

			if (proName != null) {
				proName = proName.trim();
			}

			if (proName != null && !proName.equals("")) {
				proName = proName.replace("\'", "\'\'");
				whereStr = whereStr + " and (p.productName like \'%" + proName
						+ "%\')";
			}

			if (bbCode != null) {
				bbCode = bbCode.trim();
			}

			if (bbCode != null && !bbCode.equals("")) {
				bbCode = bbCode.replace("\'", "\'\'");
				whereStr = whereStr + " and (io.bbCode like \'%" + bbCode
						+ "%\')";
			}

			if (type != null && !type.equals("")) {
				whereStr = whereStr + " and io.ioType=\'" + type + "\'";
			}

			if (inTypeId != 0L) {
				whereStr = whereStr
						+ " and io.billType=\'IN_BILL\' and io.typeId="
						+ inTypeId;
			}

			if (outTypeId != 0L) {
				whereStr = whereStr
						+ " and io.billType=\'OUT_BILL\' and io.typeId="
						+ outTypeId;
			}

			if (wcId != 0L) {
				whereStr = whereStr
						+ " and io.requType=\'WORK_CENTER\' and io.requId="
						+ wcId;
			}

			String sql = selectStr + " " + fromStr + " " + whereStr;
			if (orderCode != null && orderCode.equals("PRODUCT_CODE")) {
				sql = sql + " order by p.productCode";
			} else if (orderCode != null && orderCode.equals("IO_TIME")) {
				sql = sql + " order by io.id";
			} else {
				sql = sql + " order by io.billDate,io.id";
			}

			Session sess = null;

			List arg27;
			try {
				sess = this.getSession();
				Query q = sess.createQuery(sql);
				if (startDate != null) {
					q.setDate("startDate", startDate);
				}

				if (endDate != null) {
					q.setDate("endDate", endDate);
				}

				arg27 = q.list();
			} finally {
				if (sess != null) {
					this.releaseSession(sess);
				}

			}

			return arg27;
		}
	}

	public List<?> getInOutList(String businessBillCode, String otherType,
			long otherId) {
		String inStr = "";
		String outStr = "";
		if (otherId != 0L) {
			inStr = " and b.offerer_type=:otherType and b.offerer_id="
					+ otherId;
			outStr = " and b.requirer_type=:otherType and b.requirer_id="
					+ otherId;
		}

		String sql = "select a.* from( select b.bill_code,b.in_date as bill_date,b.offerer_type as r_type,b.offerer_id as r_id,p.product_code,p.product_name,p.unit,i.amount,\'IN\' as bill_type,i.cost from in_bill b,in_bill_item i,product p where b.id=i.bill_id and i.product_id=p.id and b.business_bill_code=:businessBillCode"
				+ inStr
				+ " and b.examined=\'Y\'"
				+ " and b.enabled=\'Y\'"
				+ " union all "
				+ " select b.bill_code,b.out_date as bill_date,b.requirer_type as r_type,b.requirer_id as r_id,p.product_code,p.product_name,p.unit,i.amount,\'OUT\' as bill_type,i.cost"
				+ " from out_bill b,out_bill_item i,product p"
				+ " where b.id=i.bill_id"
				+ " and i.product_id=p.id"
				+ " and b.business_bill_code=:businessBillCode"
				+ outStr
				+ " and b.examined=\'Y\'"
				+ " and b.enabled=\'Y\'"
				+ ") as a"
				+ " order by a.bill_date";
		Session sess = null;

		List arg10;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			q.setString("businessBillCode", businessBillCode);
			if (otherId != 0L) {
				q.setString("otherType", otherType);
			}

			arg10 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg10;
	}

	public BigDecimal getInCost(String bbCode) {
		String sql = "select sum(i.cost) from com.minierp.model.InBill as b ,com.minierp.model.InBillItem as i where b.id=i.billId and b.businessBillCode = :bbCode and b.examined=\'Y\' and b.enabled=\'Y\'";
		String[] paramNames = new String[]{"bbCode"};
		Object[] values = new Object[]{bbCode};
		List list = this.getHibernateTemplate().findByNamedParam(sql,
				paramNames, values);
		return !list.isEmpty() && list.get(0) != null ? new BigDecimal(list
				.get(0).toString()) : new BigDecimal(0);
	}

	public BigDecimal getOutCost(String bbCode) {
		String sql = "select sum(i.cost) from com.minierp.model.OutBill as b ,com.minierp.model.OutBillItem as i where b.id=i.billId and b.businessBillCode = :bbCode and b.examined=\'Y\' and b.enabled=\'Y\'";
		String[] paramNames = new String[]{"bbCode"};
		Object[] values = new Object[]{bbCode};
		List list = this.getHibernateTemplate().findByNamedParam(sql,
				paramNames, values);
		return !list.isEmpty() && list.get(0) != null ? new BigDecimal(list
				.get(0).toString()) : new BigDecimal(0);
	}

	public boolean isProductHasStock(long productId) {
		String sql = "select id from stock where amount!=0 and product_id="
				+ productId + " limit 1";
		Session sess = null;

		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			List list = q.list();
			if (list.size() > 0) {
				return true;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return false;
	}

	public boolean isStorehouseHasStock(long storehouseId) {
		String sql = "select id from stock where amount!=0 and storehouse_id="
				+ storehouseId + " limit 1";
		Session sess = null;

		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			List list = q.list();
			if (list.size() > 0) {
				return true;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return false;
	}

	public int updateIohLogData(final String billType, final long billId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.InOutHistory set logData=\'Y\' where billType=:billType and billId=:billId";
						Query q = session.createQuery(hql);
						q.setString("billType", billType);
						q.setLong("billId", billId);
						return new Integer(q.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}
}